
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Teacher extends JFrame implements ActionListener{
	

	JPanel jp_1, jp_2, jp_3, jp_4, jp_5 = null;
	JLabel jlb_1, jlb_2, jlb_3, jlb_4, jlb_5 = null;
	JButton jb_1, jb_2, jb_3, jb_4 = null;
	JTextField jtf;
	JTable jtable_1, jtable_2 = null;                                     
	DefaultTableModel model_1, model_2 = null;                            
	JScrollPane jsp_1, jsp_2 = null;                                  
	
	String tea_no;
	String tea_name;
	String sexual;
	String age;
	String salary;
	String position;
	String subject;
	
	String sno;
	String stu_name;
	String grade;
	static String dbURL = "jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false";
	static String userName="root";
	static String userPwd="0118";
	static Connection ct = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	public Teacher(String name, String tno){			
		tea_no = tno;
		

		try {
			DataSource dataSource = new MysqlDataSource();
			((MysqlDataSource) dataSource).setURL(dbURL);
			((MysqlDataSource) dataSource).setUser(userName);
			((MysqlDataSource) dataSource).setPassword(userPwd);
			ct = dataSource.getConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}
				

		jp_1 = new JPanel();
		jp_2 = new JPanel();
		jp_3 = new JPanel();
		jp_4 = new JPanel();
		jp_5 = new JPanel();



		jlb_1 = new JLabel("姓名：");
		jlb_2 = new JLabel(name);
		jlb_3 = new JLabel("职工号：");
		jlb_4 = new JLabel(tno);
		jlb_5 = new JLabel("请输入学号");


		jlb_1.setFont(new java.awt.Font("Dialog", 1, 30));
		jlb_2.setFont(new java.awt.Font("Dialog", 1, 25));
		jlb_3.setFont(new java.awt.Font("Dialog", 1, 30));
		jlb_4.setFont(new java.awt.Font("Dialog", 1, 25));
		jlb_5.setFont(new java.awt.Font("Dialog", 1, 20));
		

		jb_1 = new JButton("个人信息");
		jb_2 = new JButton("查询成绩");
		jb_3 = new JButton("修改成绩");
		jb_4 = new JButton("退出系统");
		jb_1.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_2.setFont(new java.awt.Font("Dialog", 1, 15));
		jb_3.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_4.setFont(new java.awt.Font("Dialog", 1, 20));
		jb_1.setBackground(Color.GRAY);
		jb_2.setBackground(Color.GRAY);
		jb_3.setBackground(Color.GRAY);
		jb_4.setBackground(Color.GRAY);


		jb_1.addActionListener(this);
		jb_2.addActionListener(this);
		jb_3.addActionListener(this);
		jb_4.addActionListener(this);
		

		jtf = new JTextField(10);
		

		String[] colnames_1 = {"姓名", "性别", "年龄","工资", "职称", "所教科目"};
		model_1 = new DefaultTableModel(colnames_1, 1);
		jtable_1 = new JTable(model_1);
		jsp_1 = new JScrollPane(jtable_1);
		
		String[] colnames_2 = {"姓名", "学号", "科目", "成绩"};
		model_2 = new DefaultTableModel(colnames_2, 1);
		jtable_2 = new JTable(model_2);
		jsp_2 = new JScrollPane(jtable_2);
		

		jp_1.add(jlb_1);
		jp_1.add(jlb_2);
		
		jp_2.add(jlb_3);
		jp_2.add(jlb_4);
		
		jp_3.add(jb_1);
		jp_3.add(jsp_1);
		
		jp_4.add(jlb_5);
		jp_4.add(jtf);
		jp_4.add(jb_2);
		jp_4.add(jsp_2);
		
		jp_5.add(jb_3);
		jp_5.add(jb_4);
		

		this.add(jp_1);
		this.add(jp_2);
		this.add(jp_3);
		this.add(jp_4);
		this.add(jp_5);
		

		this.setLayout(new GridLayout(5, 1));
		jp_1.setLayout(new FlowLayout(FlowLayout.LEFT));
		jp_2.setLayout(new FlowLayout(FlowLayout.LEFT));
		

		this.setTitle("教师系统");
		URL resource = Login.class.getClassLoader().getResource("河池学院.jfif");
		Image image = new ImageIcon(resource).getImage();
		this.setIconImage(image);

		this.setSize(500, 500);
							

		this.setLocationRelativeTo(null);
		this.setBackground(Color.yellow);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				

		this.setVisible(true);
		this.setResizable(true);					
	}
	

	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand() == "个人信息"){		
			try{
				ps = ct.prepareStatement("select * from Teacher where 教工号 =? ");
				ps.setString(1, tea_no);
				
				rs = ps.executeQuery();					

				while(rs.next()){						
					tea_name = rs.getString(5);				
					sexual = rs.getString(6);				
					age = rs.getString(9);				
					salary = rs.getString(7);
					position = rs.getString(8);
					subject = rs.getString(10);
				}				
			}catch (Exception e1) {					
			e1.printStackTrace();
			}

			jtable_1.setValueAt(tea_name, 0, 0);
			jtable_1.setValueAt(sexual, 0, 1);
			jtable_1.setValueAt(age, 0, 2);
			jtable_1.setValueAt(salary, 0, 3);	
			jtable_1.setValueAt(position, 0, 4);
			jtable_1.setValueAt(subject, 0, 5);
		}else if(e.getActionCommand() == "查询成绩"){
			sno = jtf.getText();
			try{
				ps = ct.prepareStatement("select * from Student where 学号 =? ");		
				ps.setString(1, sno);			
				
				rs = ps.executeQuery();					

				while(rs.next()){						
					stu_name = rs.getString(5);
					if(subject.equals("高数"))
						grade = rs.getString(6);
					else if(subject.equals("C语言"))
						grade = rs.getString(7);
					else if(subject.equals("Java"))
						grade = rs.getString(8);
					else if(subject.equals("英语"))
						grade = rs.getString(9);
				}				
			}catch (Exception e1) {					
			e1.printStackTrace();
			}

			jtable_2.setValueAt(stu_name, 0, 0);
			jtable_2.setValueAt(sno, 0, 1);
			jtable_2.setValueAt(subject, 0, 2);
			jtable_2.setValueAt(grade, 0, 3);					
		}else if(e.getActionCommand() == "修改成绩"){
			ChangeGrade cg = new ChangeGrade(sno, subject);
		}else if(e.getActionCommand() == "退出系统"){
			System.exit(0);
		}
	}
}

